package cjm.task5.controller;

import cjm.task5.pojo.Record;
import cjm.task5.pojo.User;
import cjm.task5.service.AnalysisService;
import cjm.task5.service.RecordService;
import cjm.task5.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/recordcon")
public class RecordController {

    @Autowired
    private RecordService recordService;
    @Autowired
    private UserService userService;
    @Autowired
    private AnalysisService analysisService;

    //查看用户上传记录
    @RequestMapping(value = "/checkRecord")
    public String checkRecord(Model model,@RequestParam("id") Integer id){
        List<Record> records = recordService.findRecordByOneUid(id);
        model.addAttribute("records",records);
        model.addAttribute("userid",id);

        return "OneRecord";
    }

    //查看所有上传记录
    @RequestMapping(value = "/checkRecordAll")
    public String checkRecordAll(Model model){
        //List<Record> records = recordService.findRecordAll();
        //model.addAttribute("records",records);

        return "redirect:/recordcon/searchAllRecord";
    }


    //一个用户记录页面模糊查询
    @RequestMapping(value = "/searchOneUserRecord")
    public String searchOneUserRecord(Model model, String searchKeyword, @RequestParam("uid")Integer uid){
        System.out.println(searchKeyword);
        List<Record> records = recordService.searchOneUserRecord(uid,searchKeyword);
        model.addAttribute("records",records);
        model.addAttribute("userid",uid);

        return "OneRecord";
    }

    //所有用户记录页面模糊查询
//    @RequestMapping(value = "/searchAllRecord")
//    public String searchAllRecord(Model model,String searchKeyword){
//        System.out.println(searchKeyword);
//        List<Record> records = recordService.searchAllRecord(searchKeyword);
//        model.addAttribute("records",records);
//
//        return "show";
//    }

    //上传记录排序
//    @RequestMapping(value = "RecordOrderBy")
//    public String RecordOrderBy(Model model,@RequestParam("id")Integer id){
//        List<Record> records = recordService.RecordOrderBy(id);
//        model.addAttribute("records",records);
//        return "show";
//    }

    //上传记录分析
    @RequestMapping(value = "/recordAnalysis")
    public String recordAnalysis(Model model) throws IOException {
        List<String> height = analysisService.getData1();
        List<String> middle = analysisService.getData2();
        List<Record> overRecords = recordService.overTemperature();
        List<Record> records1 = recordService.areaRecord();
        model.addAttribute("height", height);
        model.addAttribute("middle", middle);
        model.addAttribute("records1",overRecords);
        model.addAttribute("records2",records1);
        return "analysis";
    }

    private static final int pageSize = 5;
    private int id = 99;
    //public List<Record> records = recordService.findRecordAll();
    //查看所有上传记录
//    @RequestMapping(value = "/searchAllRecord")
//    public ModelAndView checkRecordAll(String searchKeyword,
//                                       @RequestParam(name = "pageNum",required = false,defaultValue = "1") int pageNum)
//    {
//        // 分页操作
//        PageHelper.startPage(pageNum,pageSize);
//        ModelAndView modelAndView  = new ModelAndView();
//
//        //1,查询出当前数据
//        //List<Record> records = this.recordService.findRecordAll();
//        List<Record> records = recordService.searchAllRecord(searchKeyword);
//
//        // 获取到分页数据
//        PageInfo page = new PageInfo(records);
//        // 获取到分页的一些信息
//        // 当前页码
//        int mypageNum = page.getPageNum();
//        // 每页显示的记录数
//        int mypageSize = page.getPageSize();
//        // 总条数
//        long total = page.getTotal();
//        // 上一页
//        int prePage = page.getPrePage();
//        // 下一页
//        int nextPage = page.getNextPage();
//        // 总页码
//        int pages = page.getPages();
//
//        //2.将数据存起来
//        // 将分页数据存起来
//        modelAndView.addObject("page",page);
//        modelAndView.addObject("records",records);
//        modelAndView.addObject("searchKeyword",searchKeyword);
//
//        //3.跳转到 页面
//        modelAndView.setViewName("show");
//        return modelAndView;
//    }

    //上传记录排序
    @RequestMapping(value = "/searchAllRecord")
    public ModelAndView RecordOrderBy(
            @RequestParam(value = "searchKeyword",required = false) String searchKeyword,
            @RequestParam(value = "id",required = false,defaultValue = "100") Integer id,
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum)
    {
        // 分页操作
        PageHelper.startPage(pageNum,pageSize);
        ModelAndView modelAndView  = new ModelAndView();

        //1,查询出当前数据
        //List<Record> records = this.recordService.findRecordAll();
        //List<Record> records = recordService.searchAllRecord(searchKeyword);
        List<Record> records = recordService.getRecordByKeyAndOrder(searchKeyword,id);
//        if(this.id!=id || this.id == 99){
//            records = recordService.getRecordByKeyAndOrder(searchKeyword,id);
//        }
//        this.id=id;

        // 获取到分页数据
        PageInfo page = new PageInfo(records);
        // 获取到分页的一些信息
        // 当前页码
        int mypageNum = page.getPageNum();
        // 每页显示的记录数
        int mypageSize = page.getPageSize();
        // 总条数
        long total = page.getTotal();
        // 上一页
        int prePage = page.getPrePage();
        // 下一页
        int nextPage = page.getNextPage();
        // 总页码
        int pages = page.getPages();

        //2.将数据存起来
        // 将分页数据存起来
        modelAndView.addObject("page",page);
        modelAndView.addObject("records",records);
        modelAndView.addObject("searchKeyword",searchKeyword);

        //3.跳转到 页面
        modelAndView.setViewName("show");
        return modelAndView;
    }

}
